﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using SaleWebsite.library;
using System.Data;

namespace SaleWebsite.Models
{
    public class Product
    {
        [DisplayName("Mã hàng(*)")]
        public ulong Id{get;set;}

        [DisplayName("Image(*)")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage="Bạn Chưa chọn hình ảnh cho sản phẩm này")]
        public string Image{get;set;}

        [DisplayName("Tên hàng(*)")]
        [DataType(DataType.Text)]
        public string Name{get;set;}

        [DisplayName("Giá 1(*)")]
        [Range(0, float.MaxValue, ErrorMessage = "Can only be between 0 .. 15")]
        public float  Price1{get;set;}

        [DisplayName("Giá 2(*)")]
        [DataType(DataType.Text)]
        public float Price2{get;set;}

        [DisplayName("Loại Hàng(*)")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage="Bạn chưa chọn loại hàng")]
        public ulong Product_type{get;set;}

        [DisplayName("Đơn Vị tính(*)")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "Bạn chưa chọn đơn vị tính")]
        public ulong Id_donvitinh{get;set;}

        [DisplayName("Miêu tả về sản phẩm (*)")]
        [DataType(DataType.Text)]
        public string Description{get;set;}

        [DisplayName("Xuất sứ(*)")]
        [DataType(DataType.Text)]
        public string Product_from{get;set;}

        [DisplayName("Nhà sản xuất(*)")]
        [DataType(DataType.Text)]
        public  string Factory{get;set;}

        [DisplayName("Khuyến mại(*)")]
        [DataType(DataType.Text)]
        public Int16 Is_Promotion { get; set; }

        [DisplayName("VAT(*)")]
        [Range(0, 100, ErrorMessage = "Can only be between 0 .. 100")]
        public float Vat{get;set;}
        public ulong Insert()
        {

            try
            {
                return MySqlImplement.GetInstance().ExcuteQuery(Params.GetAllParams(this), "product_insert");
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

        }
        public static DataSet GetAll()
        {
            return MySqlImplement.GetInstance().GetQuery("product_getall");
        }
        public int Delete()
        {
            return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this), "product_delete");
        }
        public int Save()
        {
            try
            {
                return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this), "product_update");
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetGroupById()
        {
            return MySqlImplement.GetInstance().GetQuery("group_get_id", Params.GetAllParams(this));
        }
        private static Product Convert_one(DataRow row)
        {
            return new Product()

            {
                Name = row["product_name"].ToString(),
                Id = ulong.Parse(row["product_id"].ToString()),
                Price1 = float.Parse(row["price1"].ToString()),
                Price2 = float.Parse(row["price2"].ToString()),
                Is_Promotion = Int16.Parse(row["is_promotion"].ToString()),
                Product_type = ulong.Parse(row["product_type_id"].ToString()),
                Description = row["description"].ToString(),
                Product_from = row["product_from"].ToString(),
                Factory = row["factory"].ToString(),
                Vat = float.Parse(row["vat"].ToString()),
                Id_donvitinh=ulong.Parse(row["id_donvitinh"].ToString()),
                Image=row["product_image"].ToString()

            };
        }
        public static IEnumerable<Product> ConvertAll(DataSet data)
        {
            foreach (DataRow row in data.Tables[0].Rows)
            {
                yield return Convert_one(row);
            }
        }
    }
}